<template>
	<view class="uni-popup-share">
		<view class="uni-share-title"><text class="uni-share-title-text">{{title}}</text></view>
		<view class="uni-share-content">
			<view class="uni-share-content-box">
				<view class="uni-share-content-item" v-for="(item,index) in bottomData" :key="index" @click.stop="select(item,index)">
					<image class="uni-share-image" :src="item.icon" mode="aspectFill"></image>
					<text class="uni-share-text">{{item.text}}</text>
				</view>

			</view>
		</view>
		<view class="uni-share-button-box">
			<button class="uni-share-button" @click="close">取消</button>
		</view>
	</view>
</template>

<script>
	import Api from '@/common/api';
	export default {
		name: 'UniPopupShare',
		props: {
			title: {
				type: String,
				default: '选择你要进行的操作'
			}
		},
		inject: ['popup'],
		data() {
			return {
				bottomData: [{
						text: '开大门',
						icon: '/static/door1.png',
						name: '5'
					},
					{
						text: '开包厢门',
						icon: '/static/door.png',
						name: '0'
					},
					{
						text: '续费1小时',
						icon: '/static/xufei.png',
						name: '1'
					},
					{
						text: '续费2小时',
						icon: '/static/xufei.png',
						name: '2'
					},
					{
						text: '续费3小时',
						icon: '/static/xufei.png',
						name: '3'
					},
					{
						text: '续费4小时',
						icon: '/static/xufei.png',
						name: '4'
					},
					{
						text: '续费6小时',
						icon: '/static/xufei.png',
						name: '6'
					},
					{
						text: '续费8小时',
						icon: '/static/xufei.png',
						name: '8'
					}
				]
			}
		},
		created() {},
		methods: {
			/**
			 * 选择内容
			 */
			async select(item, index) {
				console.log(uni.getStorageSync('order'))
				let orderId = uni.getStorageSync('order').id;
				let deviceId = uni.getStorageSync('order').deviceId;
				if(item.name=='5'||item.name=='0'){
					let params = {
						orderId:orderId,
						taskType:item.name
					}
					let data = await Api.apiCall('get', Api.qsg.generate, params);
					if(data){
						uni.showModal({
							title: '提示',
							showCancel: false,
							content: '操作成功.',
							success: function (res) {
								if (res.confirm) {
								}
							}
						});
					}
				}else{
					let order = uni.getStorageSync('order');
					if(order.source=='share'){
						uni.showModal({
							title: '提示',
							showCancel: false,
							content: '仅支持购买人续费',
							success: function (res) {
								if (res.confirm) {
								}
							}
						});
						return false;
					}
					let startTime = this.dateFormat(order.endTime);
					let duration = item.name;
					let endTime = this.addDays(startTime,duration);
					this.getMeal(order.id,duration,deviceId,startTime,endTime);
				}
			},
			async getMeal(orderId,duration,deviceId,startTime,endTime){
				let data = await Api.apiCall('get', Api.qsg.getMealByDuration, {orderId:orderId,duration:duration});
				if(data&&data!=0){
					this.checkIsIntersect(data,deviceId,startTime,endTime);
				}else{
					uni.showToast({
						icon:'error',
						title:'该时长暂不支持续费',
						duration:3000
					})
				}
			},
			async checkIsIntersect(meal,deviceId,startTime,endTime){
				let params = {
					startTime: startTime,
					endTime: endTime,
					price: meal.price,
					storeId: meal.storeId,
					deviceId: deviceId,
					mealId: meal.id,
					mealName: meal.mealName,
				};
					let data = await Api.apiCall('post', Api.order.checkIsIntersect, params);
					if (data && data == 1) {
						uni.showToast({
							icon: "error",
							title: "订单时间冲突"
						})
					} else {
						console.log('下一步')
						this.submit(params)
					}
			},
			async submit(params) {
				let data = await Api.apiCall('post', Api.order.createOrder, params);
				console.log(data);
				if (data) {
					let id = data.id;
					let lastId = uni.getStorageSync('order').id;
					let url = `/pages/money/pay?id=${id}&lastId=${lastId}`;
					uni.redirectTo({
						url: url
					});
				} else {
					that.$core.msg(data);
				}
			},
			dateFormat(dateStr) {
				let date = new Date(dateStr);
				let year = date.getFullYear();
				// 在日期格式中，月份是从0开始的，因此要加0，使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
				let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
				let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
				let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
				let minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
				let seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
				// 拼接
				return year + "/" + month + "/" + day + " " + hours + ":" + minutes;
			},
			addDays(date, duration) {
				var nd = new Date(date);
				nd = nd.valueOf();
				nd = nd + duration * 60 * 60 * 1000;
				nd = new Date(nd);
				//alert(nd.getFullYear() + "年" + (nd.getMonth() + 1) + "月" + nd.getDate() + "日");
				var y = nd.getFullYear();
				var m = nd.getMonth() + 1;
				var d = nd.getDate();
				if (m <= 9) m = "0" + m;
				if (d <= 9) d = "0" + d;
				var hours = nd.getHours() < 10 ? "0" + nd.getHours() : nd.getHours();
				var minutes = nd.getMinutes() < 10 ? "0" + nd.getMinutes() : nd.getMinutes();
				var seconds = nd.getSeconds() < 10 ? "0" + nd.getSeconds() : nd.getSeconds();
				return y + "/" + m + "/" + d + " " + hours + ":" + minutes;;
			},
			/**
			 * 关闭窗口
			 */
			close() {
				this.popup.close()
			}
		}
	}
</script>
<style lang="scss" scoped>
	.uni-popup-share {
		background-color: #fff;
	}
	.uni-share-title {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: center;
		height: 40px;
	}
	.uni-share-title-text {
		font-size: 14px;
		color: #666;
	}
	.uni-share-content {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: center;
		padding-top: 10px;
	}
	
	.uni-share-content-box {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		flex-wrap: wrap;
		width: 360px;
	}
	
	.uni-share-content-item {
		width: 90px;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: center;
		padding: 10px 0;
		align-items: center;
	}
	
	.uni-share-content-item:active {
		background-color: #f5f5f5;
	}
	
	.uni-share-image {
		width: 30px;
		height: 30px;
	}
	
	.uni-share-text {
		margin-top: 10px;
		font-size: 14px;
		color: #3B4144;
	}
	
	.uni-share-button-box {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		padding: 10px 15px;
	}
	
	.uni-share-button {
		flex: 1;
		border-radius: 50px;
		color: #666;
		font-size: 16px;
	}
	
	.uni-share-button::after {
		border-radius: 50px;
	}
</style>
